<?php

/**
 * Contains a class to define a Page object with common headers and footers.
 *
 * PHP Version 5
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */
 
/** @uses encode To sanitize output. */
require_once 'function_encode.php';

/**
 * A class to define a Page object with common headers and footers.
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */ 
class Page
{
    /**
     * Variable to contain the page's title.
     *
     * @access public
     * @var string
     */
    public $title;

    /**
     * Variable to contain the page's subtitle.
     * 
     * @access public
     * @var string
     */
    public $subtitle;
    
    /**
     * Variable to contain the keywords for this page.
     *
     * @access public
     * @var string
     */
    public $keywords;
    
    /**
     * Object to contain information about the user.
     *
     * @access protected
     * @var object
     */
    protected $user;
    
    /**
     * Array to contain the search radius values.
     *
     * @access public
     * @var array     Comes from constant SEARCH_RADIUS defined in presets.php
     */
    public $radius;
    
    /**
     * Database access object.
     *
     * @access protected
     * @var object
     */
    protected $db;
    
    /**
     * Constructor function.  Establishes a database connection, sets $user
     * and $radius.
     */
    public function __construct()
    {
        session_start();
        $this->db     = new Database(DBSERVER, DBUSER, DBPASS, DBNAME);
        $this->user   = $this->db->getCurrentUser();
        $this->radius = explode(',', SEARCH_RADIUS);
        if (valueOf($_SESSION, 'category') == '') {
            $_SESSION['category'] = new Category(0, 'All Categories');
        }
    }
    
    /**
     * Function to set the page title.
     *
     * @param string $title The pagetitle to set.
     *
     * @access public
     * @return null
     */
    public function setTitle($title)
    {
        $this->title = encode($title);
    }

    /**
     * Function to print the page header.
     *
     * @access public
     * @return null
     */
    public function printHeader()
    {
        /** Prints the doctype and HEAD elements. */
        echo '<!DOCTYPE html>', "\n\n",
            '<html>', "\n\n",
            '<head>', "\n",
            ' <meta http-equiv="Content-Type" content="text/html;',
            'charset=UTF-8" />', "\n",
            ' <title>', SITENAME, ': ' . $this->title . '</title>', "\n",
            ' <link rel="stylesheet" type="text/css" ',
            'href="', RELATIVE_PATH, 'stylesheets/main.css" />', "\n",
            ' <!--[if IE 7]>', "\n",
            ' <link rel="stylesheet" type="text/css" ',
            'href="', RELATIVE_PATH, 'stylesheets/ie7hacks.css" />', "\n",
            ' <![endif]-->', "\n",
            ' <!--[if IE 6]>', "\n",
            ' <link rel="stylesheet" type="text/css" ',
            'href="', RELATIVE_PATH, 'stylesheets/ie6hacks.css" />', "\n",
            ' <![endif]-->', "\n",
            ' <link rel="stylesheet" type="text/css" ',
            'href="', RELATIVE_PATH, 'stylesheets/jquery-calendar.css" />',
            "\n",
            ' <script type="text/javascript" src="', RELATIVE_PATH, 
            'scripts/footer.js"></script>', "\n",
            ' <script type="text/javascript" src="', RELATIVE_PATH, 
            'scripts/jquery.js"></script>', "\n",
            ' <script type="text/javascript" src="', RELATIVE_PATH, 
            'scripts/jquery-calendar.js"></script>', "\n",
            '</head>', "\n\n";
            
        /** Prints the body and skip to content elements. */
        echo '<body onload="adjust();">', "\n\n",
            '<p class="hidden"><a href="#content">Skip to Content</a></p>',
            "\n\n";
            
        /**
         * Prints the header box.
         *
         * Contains the site logo (via CSS), the login/logout and registration
         * links, and the search box.
         */
        echo '<div id="header">', "\n",
            ' <h1><a href="' .BASE_URL. '">', SITENAME, '</a></h1>', "\n",
            ' <p class="print">URL: ', encode(PAGE_URL),'</p>', "\n",
            ' <div class="loginregister">', "\n",
            '  <a href="', RELATIVE_PATH, 'login.php">';
        if ($this->user->role == R_GUEST) {
            echo 'Log In</a>', "\n",
               '  or <a href="', RELATIVE_PATH, 'register.php">Register';
        } else {
            echo 'Log Out';
        }
        echo '</a>', "\n";
        echo ' </div>', "\n",
            ' <form id="search" method="get" ',
            'action="', RELATIVE_PATH, 'search.php">', "\n",
            '  <div>', "\n",
            '   <label for="query">Search For:</label>', "\n",
            '   <input name="query" id="query" type="text" />', "\n",
            '   <label for="radius">Within:</label>', "\n",
            '   <select name="radius" id="radius">', "\n";
        foreach ($this->radius as $radius) {
            echo '    <option value="', $radius, '"';
            if (valueOf($_SESSION, 'radius') == $radius) {
                echo ' selected="selected"';
            }
            echo '>', $radius, ' ' , D_DISTANCE, '</option>', "\n";
        }
        echo '   </select>', "\n",
            '   <label for="zip">Of Zip:</label>', "\n",
            '   <input name="zip" id="zip" type="text" value="',
            $this->user->zip->zip, '" />', "\n",
            '   <input type="submit" value="Go" class="button" />', "\n",
            '  </div>', "\n",
            ' </form>', "\n",
            '</div>', "\n\n";
            
        /** Prints the menu. */
        $this->_printMenu();
        
        /** Prints the opening of the content box and the page title. */
        echo '<div id="content">', "\n",
            ' <h1>', $this->title, '</h1>', "\n";
    }
    
    /**
     * Function for printing the navigation.
     *
     * @access private
     * @return null
     */
    private function _printMenu()
    {
        /** Sets state variables. */
        $isUser          = ($this->user->role != R_GUEST);
        $isOwner         = ($this->user->role == R_OWNER);
        $isAdmin         = ($this->user->role == R_ADMIN);
        $isAdminAndOwner = (
            $isAdmin 
            && $this->db->isStoreOwner($this->user->id)
        );
        
        /** Prints the menus. */
        echo '<div id="navigation">', "\n",
            ' <h1 class="hidden">Navigation</h1>', "\n";
        if ($isUser) {
            $this->_printMenuUser();
        }
        if ($isOwner || $isAdminAndOwner) {
            $this->_printMenuOwner();
        }
        if ($isAdmin) {
            $this->_printMenuAdmin();
        }
        $this->_printMenuCategories();
        $this->_printMenuEvents();
        $this->_printMenuInfo();
        echo '</div>', "\n\n";
    }
    
    /**
     * Prints the user menu.
     *
     * @access private
     * @return null
     */
    private function _printMenuUser()
    {
        echo ' <h2>User</h2>', "\n",
            ' <ul class="navigation">', "\n",
            '  <li><a href="', RELATIVE_PATH, 'user/profile.php">',
            'My Account</a></li>', "\n",
            '  <li><a href="', RELATIVE_PATH, 'user/create_store.php">',
            'Create a Store</a></li>', "\n",
            '  <li><a href="', RELATIVE_PATH, 'user/create_event.php">',
            'Create an Event</a></li>', "\n",
            ' </ul>', "\n";
    }
    
    /**
     * Prints the store owner menu.
     *
     * @access private
     * @return null
     */
    private function _printMenuOwner()
    {
        echo ' <h2>Store Owner</h2>', "\n",
            ' <ul class="navigation">', "\n",
            '  <li><a href="', RELATIVE_PATH, 'owner/stores.php">',
            'My Stores</a></li>', "\n",
            '  <li><a href="', RELATIVE_PATH, 'owner/events.php">',
            'My Events</a></li>', "\n",
            /*'  <li><a href="', RELATIVE_PATH, 'owner/products.php">',
            'My Products</a></li>', "\n",
            '  <li><a href="', RELATIVE_PATH, 'owner/sales.php">',
            'My Sales</a></li>', "\n",
            '  <li><a href="', RELATIVE_PATH, 'owner/help.php">',
            'Help</a></li>', "\n",*/
            ' </ul>', "\n";
    }
    
    /**
     * Prints the administrator menu.
     *
     * @access private
     * @return null
     */
    private function _printMenuAdmin()
    {
        echo ' <h2>Administrator</h2>', "\n",
            ' <ul class="navigation">', "\n",
            '  <li><a href="', RELATIVE_PATH, 'admin/users.php">',
            'Manage Users</a></li>', "\n",
            /*'  <li><a href="', RELATIVE_PATH, 'admin/flags.php">',
            'Manage Flags</a></li>', "\n",*/
            '  <li><a href="', RELATIVE_PATH, 'admin/stores.php">',
            'Manage Stores</a></li>', "\n",
            '  <li><a href="', RELATIVE_PATH, 'admin/events.php">',
            'Manage Events</a></li>', "\n",
            '  <li><a href="', RELATIVE_PATH, 'admin/categories.php">',
            'Manage Categories</a></li>', "\n",
            ' </ul>', "\n";
    }
    
    /**
     * Prints the categories menu.
     *
     * @access private
     * @return null
     */
    private function _printMenuCategories()
    {
        $categoryId = $_SESSION['category']->id;
        $categories = $this->db->getCategoriesByParentId($categoryId);
        if ($_SESSION['category']->id != 0) {
            echo ' <h2><a href="', RELATIVE_PATH, 'browse.php?categoryid=',
                $_SESSION['category']->id, '">', 
                encode($_SESSION['category']->name), '</a></h2>', "\n";
        } else {
            echo ' <h2><a href="', RELATIVE_PATH, 'browse.php">',
                'Browse Stores</a></h2>', "\n";
        }
        echo ' <ul class="navigation">', "\n";
        if ($categoryId > 0) {
            echo '  <li class="all"><a href="', RELATIVE_PATH, 
                'browse.php?categoryid=0">All Categories</a></li>', "\n";
        }
        foreach ($categories as $category) {
            echo '  <li><a href="', RELATIVE_PATH, 'browse.php?categoryid=',
                $category->id, '">', encode($category->name), '</a> (',
                $this->db->getCategoryCount($category->id), ')</li>', "\n";
        }
        echo ' </ul>', "\n";
    }
    
    /**
     * Prints the events menu.
     *
     * @access private
     * @return null
     */
    private function _printMenuEvents()
    {
        echo ' <h2><a href="', RELATIVE_PATH, 'events.php">',
            'Browse Events</a></h2>', "\n";
    }
    
    /**
     * Prints the info menu.
     *
     * @access private
     * @return null
     */
    private function _printMenuInfo()
    {
        echo ' <h2>Information</h2>', "\n",
            ' <ul class="navigation">', "\n",
            '  <li><a href="', RELATIVE_PATH, 'info/about.php">',
            'About ', SITENAME, '</a></li>', "\n",
            '  <li><a href="', RELATIVE_PATH, 'info/project.php">',
            'About the Project</a></li>', "\n",
            '  <li><a href="', RELATIVE_PATH, 'info/faq_customer.php">',
            'Customer FAQ</a></li>', "\n",
            '  <li><a href="', RELATIVE_PATH, 'info/faq_vendor.php">',
            'Vendor FAQ</a></li>', "\n",
            '  <li><a href="', RELATIVE_PATH, 'info/events.php">',
            'Hosting an Event?</a></li>', "\n",
            '  <li><a href="', RELATIVE_PATH, 'info/events.php">',
            'Holding a Yard Sale?',
            '</a></li>', "\n",
            ' </ul>', "\n";
    }
    
    /**
     * Function for printing the footer.
     *
     * @access public
     * @return null
     */
    public function printFooter()
    {
        echo '</div>', "\n\n",
            '<div id="footer">', "\n",
            ' <ul>', "\n",
            '  <li><a href="', RELATIVE_PATH, 'index.php">', 
            SITENAME, ' Home</a></li>', "\n",
            '  <li><a href="', RELATIVE_PATH, 'info/privacy.php">',
            'Privacy Policy</a></li>', "\n",
            '  <li><a href="', RELATIVE_PATH, 'info/tou.php">',
            'Terms of Use</a></li>', "\n",
            ' </ul>', "\n",
            '</div>', "\n\n",
            '</body>', "\n\n",
            '</html>';
    }
}

?>